package Interface.Relation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;

import Controleur.ControleurGestionRelation;
import Controleur.ControleurGestionTable;
import Interface.PanneauRelation;
import Metier.Attribut;
import Metier.Table;

public class InterfaceTable extends JPanel implements MouseMotionListener,
MouseListener, ActionListener {
	private JPopupMenu jpm = new JPopupMenu();
	private JMenuItem itemMasquer = new JMenuItem("Masquer la table");
	
	JList list;
	private PanneauRelation panneauRelation;
	private Table table;
	private int x;
	private int y;
	private ControleurGestionRelation controleur;
	private int button;
	public void setPosition(Point position) {
		controleur.setPosition(table,position);
	}

	public InterfaceTable(Table table, PanneauRelation pRelation) {
		this.table = table;
		this.setName(table.getNom());

		this.panneauRelation = pRelation;
		this.setLayout(new BorderLayout());
		JPanel pan = new JPanel();
		pan.setBackground(Color.black);
		pan.addMouseMotionListener(this);
		pan.addMouseListener(this);

		add(pan, BorderLayout.NORTH);

		JLabel labName = new JLabel(table.getNom());
		labName.setHorizontalAlignment(JLabel.CENTER);
		labName.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		labName.addMouseMotionListener(this);
		labName.addMouseListener(this);
		add(labName, BorderLayout.NORTH);
		controleur = ControleurGestionRelation.getInstance(); 
		List<Boolean> listCle = controleur.getPrimaryKey(table.getNom(),table.getAllAttribut());

		list = new JList(table.getAllAttribut().toArray());

		list.setName(table.getNom());
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.addMouseListener(new DragAndDropListener());
		list.setTransferHandler(new TableTransferHandler(panneauRelation,this.table));

		list.setPreferredSize(new Dimension(100,table.getAllAttribut().size()*18));
		list.setCellRenderer(new MyCellRenderer(listCle));
		add(list);
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		controleur.setLargeur(table,this.getPreferredSize().getWidth());
		this.itemMasquer.addActionListener(this);
		
	}

	public void mouseDragged(MouseEvent event) {
		if (this.button==event.BUTTON1)
		{
		controleur.setPosition(table,new Point(/*this.table.getX() +*/ x+event.getXOnScreen(),y+/* this.table.getY()
				+*/ event.getYOnScreen()));
		this.panneauRelation.repaint();
		}
	}

	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mouseClicked(MouseEvent event) {
		if(event.getButton()==event.BUTTON3)
		{
			jpm.add(this.itemMasquer);
			jpm.show(this, event.getX(), event.getY());

		}
	}

	public void mouseEntered(MouseEvent arg0) {
		if (this.panneauRelation.getCursor().getType() == Cursor.DEFAULT_CURSOR)
			this.panneauRelation.setCursor(Cursor
					.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	public void mouseExited(MouseEvent arg0) {
		if (this.panneauRelation.getCursor().getType() == Cursor.HAND_CURSOR)
			this.panneauRelation.setCursor(Cursor.getDefaultCursor());
	}

	public void mousePressed(MouseEvent event) {

		this.button = event.getButton();
		if (this.button==event.BUTTON1)
		{
			this.panneauRelation.setCursor(Cursor
					.getPredefinedCursor(Cursor.MOVE_CURSOR));
			x = table.getX() - event.getXOnScreen();
			y = table.getY() - event.getYOnScreen();
		}
	}

	public void mouseReleased(MouseEvent event) {
		if (event.getButton()==MouseEvent.BUTTON1)
		{
			if (this.panneauRelation.getCursor().getType() == Cursor.MOVE_CURSOR)
				this.panneauRelation.setCursor(Cursor
						.getPredefinedCursor(Cursor.HAND_CURSOR));
			this.rediriger();
			this.panneauRelation.repaint();
		}
	}
	
	public void rediriger() 
	{
		
		if (table.getY() > (this.panneauRelation.getHeight() - this
				.getHeight()))
			controleur.setPosition(this.table,table.getX(), this.panneauRelation.getHeight()
					- this.getHeight());
		if (table.getY() < 0)
			controleur.setPosition(this.table,table.getX(), 0);

		if (table.getX() > (this.panneauRelation.getWidth() - this.table.getWidth()))
			controleur.setPosition(this.table,
					this.panneauRelation.getWidth() - this.table.getWidth(),
					table.getY());

		if (table.getX() < 0)
			controleur.setPosition(this.table,0, table.getY());
		
	}

	public String getAttribut(int index) {
		return this.table.getAllAttribut().get(index);
	}

	public int getIndex(Attribut attribut) {
		int resultat = -1;
		for(int i=0;i<this.table.getAllAttribut().size();i++)
		{
			if (this.table.getAllAttribut().get(i).equals(attribut.getNom()))
				resultat = i; 
		}
		return resultat;
	}

	public Point getPosition() {
		return this.table.getLocation();
	}

	public void actionPerformed(ActionEvent event) {
		Object action = event.getSource();
		
		if (action == this.itemMasquer)
		{
			this.panneauRelation.masquer(this.table);
		}
		
	}

	public void update() {
		this.controleur.miseAJour(table);
		this.remove(list);
		List<Boolean> listCle = controleur.getPrimaryKey(table.getNom(),table.getAllAttribut());

		list = new JList(table.getAllAttribut().toArray());

		list.setName(table.getNom());
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.addMouseListener(new DragAndDropListener());
		list.setTransferHandler(new TableTransferHandler(panneauRelation,this.table));

		list.setPreferredSize(new Dimension(100,table.getAllAttribut().size()*18));
		list.setCellRenderer(new MyCellRenderer(listCle));
		add(list);
	}

	public void decalage(Point decalage) {
		this.controleur.setPosition(table, decalage);
		this.rediriger();
	}

	public int getTableWidth() {
		return table.getWidth();
	}

	public int getTableX() {
		return table.getX();
	}

}
